﻿Imports System.Data.SqlClient

Public Class register
    Inherits System.Web.UI.Page
    Private objrd As SqlDataReader = Nothing
    Private objexecute As New GetData
    Private sSQL As String
    Private ps As New PreparedStatement(sSQL)
    Dim strErr As String = ""

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim conn As SqlConnection = objexecute.getConn()
        If Request("btnCreate") <> Nothing And Request("btnCreate") <> "" Then
            Dim strEmail, pwd1, pwd2, cname, uname, gender, strPhone, cphone As String
            Dim pfield, strCountry, strState, uid, cid, uuid, ccid As String
            Dim isneed As Integer
            Dim sh As New SHA1

            strEmail = email.Text
            pwd1 = pswd.Text
            pwd2 = confirmpswd.Text
            cname = CompName.Text
            uname = userName.Text
            strPhone = phone.Text
            If male.Checked = True Then
                gender = "0"
            Else
                gender = "1"
            End If
            If isInformation.Checked = True Then
                isneed = 1
            Else
                isneed = 0
            End If
            cphone = cellphone.Text
            pfield = productField.SelectedValue
            strCountry = Request("country")
            strState = Request("state")

            ps.addParameter("email", strEmail)
            sSQL = "select email from member where email=@email"
            ps.strSql = sSQL
            objrd = objexecute.dr(ps, strErr, conn, objrd)
            emailErr.Text = ""
            If objrd.HasRows Then

                emailErr.Text = "This email is existed"
                objexecute.closeConn(conn)

                Exit Sub
            End If
            If strEmail = "" Then
                emailErr.Text = ""
                emailErr.Text = "This information is required"
                objexecute.closeConn(conn)

                Exit Sub
            End If
            pwdErr.Text = ""
            If pwd1 = "" Then

                pwdErr.Text = "This information is required"
                objexecute.closeConn(conn)

                Exit Sub
            End If
            cnameErr.Text = ""
            If cname = "" Then

                cnameErr.Text = "This information is required"
                objexecute.closeConn(conn)

                Exit Sub
            End If
            unameErr.Text = ""
            If uname = "" Then

                unameErr.Text = "This information is required"
                objexecute.closeConn(conn)

                Exit Sub
            End If
            phoneErr.Text = ""
            If strPhone = "" Then

                phoneErr.Text = "This information is required"
                objexecute.closeConn(conn)

                Exit Sub
            End If
            industryErr.Text = ""
            If productField.SelectedValue = "" Then

                industryErr.Text = "Please chooce the industry"
                objexecute.closeConn(conn)

                Exit Sub
            End If
            countryErr.Text = ""
            If strCountry = "" Then

                countryErr.Text = "Please choose the country"
                objexecute.closeConn(conn)

                Exit Sub
            End If
            stateErr.Text = ""
            If strState = "" Then

                stateErr.Text = "Please choose the state"
                objexecute.closeConn(conn)

                Exit Sub
            End If
            pwdErr.Text = ""
            If pwd1 <> pwd2 Then

                pwdErr.Text = "The password you input twice is not the same"
                objexecute.closeConn(conn)

                Exit Sub
            End If
            validateInfo.Text = ""
            If UCase(validate.Text) <> CStr(Session("vnum")) Then

                validateInfo.Text = "Verification  code wrong"
                objexecute.closeConn(conn)

                Exit Sub
            End If

            uid = System.Guid.NewGuid().ToString()
            cid = System.Guid.NewGuid().ToString()
            uuid = System.Guid.NewGuid().ToString()
            ccid = System.Guid.NewGuid().ToString()
            ps.addParameter("uid", uid)
            ps.addParameter("cid", cid)
            ps.addParameter("uuid", uuid)
            ps.addParameter("ccid", ccid)
            ps.addParameter("pwd", sh.mySHA(pwd1))
            ps.addParameter("active", 1)
            ps.addParameter("createDate", Now)
            ps.addParameter("gender", gender)
            ps.addParameter("phone", strPhone)
            ps.addParameter("cellphone", cphone)
            ps.addParameter("languageFlag", "en")
            ps.addParameter("userName", uname)
            ps.addParameter("companyName", cname)
            ps.addParameter("productField", pfield)
            ps.addParameter("country", strCountry)
            ps.addParameter("state", strState)
            ps.addParameter("isneed", isneed)


            sSQL = "insert into member(GAPortal_ID,company_ID,password,email,active,create_Date,gender,phone,cellphone,isneed)"
            sSQL = sSQL & " values (@uid,@cid,@pwd,@email,@active,@createDate,@gender,@phone,@cellphone,@isneed)"

            ps.strSql = sSQL
            objexecute.es(ps, strErr)

            sSQL = "insert into member_info(id,member_id,member_name,lang_flag)"
            sSQL = sSQL & " values (@uuid,@uid,@userName,@languageFlag)"

            ps.strSql = sSQL
            objexecute.es(ps, strErr)

            sSQL = "insert into merchant(company_id,active,owner_id)"
            sSQL = sSQL & " values (@cid,@active,@uid)"

            ps.strSql = sSQL
            objexecute.es(ps, strErr)

            sSQL = "insert into merchant_info(id,company_id,company_name,product_field,country,state,lang_flag)"
            sSQL = sSQL & " values (@ccid,@cid,@companyName,@productField,@country,@state,@languageFlag)"

            ps.strSql = sSQL
            objexecute.es(ps, strErr)

            If strErr = Nothing Or strErr = "" Then
                Session("uid") = uid
                Session("cid") = cid
                Response.Redirect("registersuccess.aspx")

            End If
            objexecute.closeConn(conn)
            Exit Sub
        End If
        sSQL = "select * from Category where lang_flag='en'"
        ps.strSql = sSQL

        objrd = objexecute.dr(ps, strErr, conn, objrd)
        While (objrd.Read())
            productField.Items.Add(New ListItem(
             objrd("Category_Name").ToString(), objrd("Category_ID").ToString()))
        End While
        objexecute.closeConn(conn)
    End Sub

End Class